fix(ndb): replace deprecated datetime.utcnow() and utcfromtimestamp() - #18186
Open
skippdot wants to merge 1 commit into
Open
fix(ndb): replace deprecated datetime.utcnow() and utcfromtimestamp()#18186skippdot wants to merge 1 commit into
skippdot wants to merge 1 commit into
Conversation
- Route the `_now()` staticmethods of DateTimeProperty, DateProperty and TimeProperty through a private `_utcnow()` helper in model.py, and build the epoch constant in `_legacy_db_get_value()` directly instead of via `utcfromtimestamp()` - Both APIs are deprecated since Python 3.12 and scheduled for removal; every `auto_now`/`auto_now_add` put currently raises a DeprecationWarning, and the legacy value decoder raises another - The helper strips `tzinfo` after calling `now(timezone.utc)`, because `DateTimeProperty._validate()` rejects offset-aware values unless the property is configured with `tzinfo` Fixes googleapis#15840
Contributor
There was a problem hiding this comment.
Code Review
This pull request replaces deprecated datetime.datetime.utcnow() and datetime.datetime.utcfromtimestamp() calls with Python 3.12-compatible alternatives. A new helper function _utcnow() is introduced to return naive UTC datetimes, preserving the expected behavior of ndb which stores and compares naive datetimes. The changes are accompanied by updated unit and system tests. There are no review comments to address, and the implementation looks solid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15840
datetime.datetime.utcnow()andutcfromtimestamp()are deprecated since Python 3.12 and scheduled for removal. Ingoogle-cloud-ndbthey are called from the_now()staticmethods ofDateTimeProperty,DatePropertyandTimeProperty, so everyauto_now/auto_now_addput currently emits aDeprecationWarning; the legacy value decoder emits another one for its epoch constant. Running the unit suite on Python 3.14 produced 70 such warnings.Changes
_utcnow()helper tomodel.py, next to_getfullargspec, and route the three_now()methods through it._EPOCHconstant in_legacy_db_get_value()asdatetime.datetime(1970, 1, 1)instead ofutcfromtimestamp(0)(same value).test__utcnow.Why the helper strips
tzinfoThe recommended replacement,
datetime.now(timezone.utc), returns an offset-aware value, butDateTimeProperty._validate()rejects offset-aware values unless the property is configured withtzinfo. A plain substitution would therefore makeauto_nowraiseBadValueErrorfor every property withouttzinfo. The helper calls.replace(tzinfo=None)to keep returning the naive-UTC valuendbhas always stored and compared — the same approachgoogle-authtakes in_helpers.utcnow().Verification
nox -s unit-equivalent run on Python 3.10 and 3.14: 1835 passed.utcnow/utcfromtimestampwarnings: 70 → 0.ruff format --checkandflake8clean.test_insert_datetime_property_with_tznow uses the same instant for both the aware and the naive value.